# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division, absolute_import
from src import res
from src import game

class Sprite(object):
    """plain sprite"""
    def __init__(self, parent=None, img='_default'):
        if not parent:
            raise NotImplementedError
        self.parent = parent
        img_name = parent.classname + img if img.startswith('_') else img # FIXME XXX
        self.img = res.images[img_name]
    def draw(self):
        x = (self.parent.x + game.vp.x_shift) % game.field.w_px
        y = (self.parent.y + game.vp.y_shift) % game.field.h_px
        self.img.blit(x, y)
    def __getstate__(self):
        state = self.__dict__.copy()
        state['img'] = res.to_resid(self.img)
        return state
    def __setstate__(self, state):
        img = res.from_resid(state['img'])
        state['img'] = img
        self.__dict__.update(state)


class DirSprite(object):
    """a grid sprite with direction parameter"""
    def __init__(self, parent=None, img_seq='_default', dir_step=15):
        if not parent:
            raise NotImplementedError
            # this object actually MUST have a parent, becoze it graphics
        self.parent = parent
        if type(img_seq) is not str:
            self.img_seq = img_seq
            raise NotImplementedError
        elif type(img_seq) is str:
            img_name = parent.classname + img_seq if img_seq.startswith('_') else img_seq
            self.img_seq = res.images.get(img_name, list(range(24)))
            self.img_name = img_name
        self._dir = 0
        self.dir_step = dir_step
    def _set_dir(self, val): # warn fixme
        if self.parent:
            self.parent.dir = val
        else:
            self._dir = val % 360
    dir = property(lambda self: self.parent.dir if self.parent else self._dir, _set_dir)
    @property
    def img(self):
        img = self.img_seq[int(self.parent.dir) // self.dir_step]
        img.anchor_x = self.img_seq.anchor_x # fixme
        img.anchor_y = self.img_seq.anchor_x
        return img
    def unparent(self):
        """rebind the object to a dummy entity"""
        from src import entity
        p = self.parent
        px, py, pdir = p.x, p.y, p.dir
        self.parent = entity.Entity(x=px, y=py, dir=pdir)
    def draw(self):
        x = (self.parent.x + game.vp.x_shift) % game.field.w_px
        y = (self.parent.y + game.vp.y_shift) % game.field.h_px
        self.img.blit(x, y)
        #print 'drawn at', x, self.parent.x, game.field.vp.x_shift, game.field.vp.x_shift

